package com.site.util;

import java.util.Calendar;

/**
 * 公共类，设置一些公共的方法和变量
 * @author Believe fate
 *
 */
public class Common {
	// 网站根路径
	//public static String base_url = "http://192.168.137.5:8080/site";
	public static String base_url = "http://localhost:8080/site";
	// 分页，每页记录数
    public static int pageSize = 10;
    
    /** 
     * 截取一段字符的长度(汉、日、韩文字符长度为2),不区分中英文,如果数字不正好，则少取一个字符位 
     * @param str 原始字符串 
     * @param specialCharsLength 截取长度(汉、日、韩文字符长度为2) 
     * @return 
     */  
    public static String StringCut(String str, int specialCharsLength,String cup) {
    	if(cup.length() <= 0){
    		cup = "...";
    	}
        if (str == null || "".equals(str) || specialCharsLength < 1) {  
            return "";  
        }  
        char[] chars = str.toCharArray();  
        int charsLength = getCharsLength(chars, specialCharsLength);
        int stringLength = getStringLength(chars);
        if(stringLength <= specialCharsLength){
        	return str;
        }else{
        	return new String(chars, 0, charsLength)+cup;
        }    	  
    }
    /**
     * 得到原字符串的长度 (中文占两个长度 )
     * @param chars
     * @return
     */
    private static int getStringLength(char[] chars) {  
        int count = 0;  
        for (int i = 0; i < chars.length; i++) {  
            int specialCharLength = getSpecialCharLength(chars[i]); 
            count += specialCharLength;  
        }  
        return count;  
    }
    /** 
     * 获取一段字符的长度，输入长度中汉、日、韩文字符长度为2，输出长度中所有字符均长度为1 
     * @param chars 一段字符 
     * @param specialCharsLength 输入长度，汉、日、韩文字符长度为2 
     * @return 输出长度，所有字符均长度为1 
     */  
    private static int getCharsLength(char[] chars, int specialCharsLength) {  
        int count = 0;  
        int normalCharsLength = 0;  
        for (int i = 0; i < chars.length; i++) {  
            int specialCharLength = getSpecialCharLength(chars[i]);  
            if (count <= specialCharsLength - specialCharLength) {  
                count += specialCharLength;  
                normalCharsLength++;  
            } else {  
                break;  
            }  
        }  
        return normalCharsLength;  
    }  
   /** 
     * 获取字符长度：汉、日、韩文字符长度为2，ASCII码等字符长度为1 
     * @param c 字符 
     * @return 字符长度 
     */  
    private static int getSpecialCharLength(char c) {  
        if (isLetter(c)) {  
            return 1;  
        } else {  
            return 2;  
        }  
    }  
    /** 
     * 判断一个字符是Ascill字符还是其它字符（如汉，日，韩文字符） 
     *  
     * @param char c, 需要判断的字符 
     * @return boolean, 返回true,Ascill字符 
     */  
    private static boolean isLetter(char c) {  
        int k = 0x80;  
        return c / k == 0 ? true : false;  
    }
    /**
     * 以时间来生成编号
     */
    public static  String number() {
        Calendar calCurrent = Calendar.getInstance();     
        int intDay = calCurrent.get(Calendar.DATE);     
        int intMonth = calCurrent.get(Calendar.MONTH) + 1;     
        int intYear = calCurrent.get(Calendar.YEAR);    
        int intMinute = calCurrent.get(Calendar.MINUTE);
        int intSecond = calCurrent.get(Calendar.SECOND);
        return intYear+""+intMonth+""+intDay+""+intMinute+""+intSecond;
    }
    public static void main(String[] args) {
    	number();
	}
}
